﻿using CQRSLearn.Features.Products.Dtos;
using CQRSLearn.Persistence;
using MediatR;
using Microsoft.EntityFrameworkCore;

namespace CQRSLearn.Features.Products.Queries.List
{
    public class ListProductsQueryHandler : IRequestHandler<ListProductsQuery, List<ProductDto>>
    {
        private readonly AppDbContext context;

        public ListProductsQueryHandler(AppDbContext context)
        {
            this.context = context;
        }

        public async Task<List<ProductDto>> Handle(ListProductsQuery request, CancellationToken cancellationToken)
        {
            return await context.Products
                .Select(p => new ProductDto(p.Id, p.Name, p.Description, p.Price))
                .ToListAsync();
        }
    }
}
